package com.maykk.stu_manager.global;

import java.sql.*;

/**
 * @ClassName: JdbcUtils
 * @Description: TODO Jdbc工具类
 * @Author: MaykkStar@163.com
 */
public class JdbcUtils {
    private static final String URl = "jdbc:mysql://localhost:3306/maykk?useSSL=false";
    private static final String USERNAME = "root";
    private static final String PASSWD = "123456";

    /**
     * 注册驱动
     */
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 获取连接对象
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URl, USERNAME, PASSWD);
    }

    // 释放资源
    public static void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (null != rs) {
                rs.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (null != st) {
                    st.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    if (null != conn) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
